package inno.specificSelection;


import inno.treeRepro.Chromosome;

import java.util.ArrayList;
import java.util.Random;

public class TournamentSelection implements Selection{

	double key = 0.75;
	ArrayList<Chromosome> chromoAns;
	
	@Override
	public ArrayList<Chromosome> doSelection(ArrayList<Chromosome> population) {
		
		chromoAns = new ArrayList<Chromosome>();
		
		role(population);
		role(population);
		
		return chromoAns;
	}
	
	private void role(ArrayList<Chromosome> population){
		
		boolean chooseBigger = (key>Math.random());
		
		Random rand = new Random();
		
		Chromosome ans1 = population.get(rand.nextInt(population.size()));
		Chromosome ans2 = population.get(rand.nextInt(population.size()));
		Chromosome bigger,smaller;
		
		if (ans1.getMyFitness()>ans2.getMyFitness()){
			bigger = ans1;
			smaller = ans2;
		}
		else{
			bigger = ans2;
			smaller = ans1;
		}
		
		if (true == chooseBigger){
			chromoAns.add(bigger);
		}
		else{
			chromoAns.add(smaller);
		}
	}
	

}
